// pages/article.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    notfavorimg: 'http://img.gotohard.cn/img/icon/点赞未选中.png',
    favorimg: 'http://img.gotohard.cn/img/icon/点赞选中.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var app = getApp();
    if (app.globalData.openid==null){
      app.globalData.openid ='oBrcH0TSSW8ioywUfNFK_zTj-63U'
    }
    var testUrl = app.testUrl;
    var that = this;
    //获取文章详情
    wx.request({
      url: testUrl + '/article/findtheOne.do?id=' + options.id + '&userid=' + app.globalData.openid,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var app=getApp()
        if (res.data.articleImg!=''){
          res.data.articleImg = JSON.parse(res.data.articleImg);
        }
       
        res.data.createTime = app.dateto(res.data.createTime)
        for(var i=0;i<res.data.articleCommentList.length;i++){
          res.data.articleCommentList[i].commentTime = app.dateto(res.data.articleCommentList[i].commentTime)
        }
        that.setData({
          article: res.data,
          articleCommentList : res.data.articleCommentList
        });
        console.log(that.data);

      }
    });

  },
  comment: function () {
    var app = getApp();
    var testUrl = app.testUrl;
    var that = this;

    wx.request({
      url: testUrl + '/articleComment/add.do',
      method:"post",
      data: {
        "articleId": that.data.article.id,
        "userId": app.globalData.openid,
        "content": that.data.commentContent,
       },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        if(res.data.success==true){
          that.openToast()
          wx.request({
            url: testUrl + '/article/findtheOne.do?id=' + that.data.article.id + '&userid=' + app.globalData.openid,
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: function (res) {
              var app = getApp()
              res.data.articleImg = JSON.parse(res.data.articleImg);
              res.data.createTime = app.dateto(res.data.createTime)
              for (var i = 0; i < res.data.articleCommentList.length; i++) {
                res.data.articleCommentList[i].commentTime = app.dateto(res.data.articleCommentList[i].commentTime)
              }
              that.setData({
                articleCommentList : res.data.articleCommentList
              });
              console.log(that.data);

            }
          });


        }


      }
    });
  },
  inputComment: function (e) {
    var app = getApp();
    var testUrl = app.testUrl;
    var that = this;
    that.setData({
      commentContent: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

getdata:function(){
  var app = getApp();
  var testUrl = app.testUrl;
  var that = this;
  //获取文章详情
  wx.request({
    url: testUrl + '/article/findtheOne.do?id=' +that.data.article.id + '&userid=' + app.globalData.openid,
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      var app = getApp()
      if (res.data.articleImg != '') {
        res.data.articleImg = JSON.parse(res.data.articleImg);
      }

      res.data.createTime = app.dateto(res.data.createTime)
      for (var i = 0; i < res.data.articleCommentList.length; i++) {
        res.data.articleCommentList[i].commentTime = app.dateto(res.data.articleCommentList[i].commentTime)
      }
      that.setData({
        article: res.data,
        articleCommentList: res.data.articleCommentList
      });
      console.log(that.data);

    }
  });
},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  openToast: function () {
    wx.showToast({
      title: '评论成功',
      icon: 'success',
      duration: 3000
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  addfavor: function (e) {
    var articleId = e.currentTarget.dataset.id;//文章id
    var app = getApp();
    var testUrl = app.testUrl;
    var that = this;
    var articleList;
    //获取所有文章
    wx.request({
      url: testUrl + '/article/addfavour.do',
      method: 'POST',
      data: {
        id: articleId,
        userid: app.globalData.openid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        if (res.data.success == true) {
          that.getdata()
        }
      }
    });







  },
  subfavor: function (e) {
    var articleId = e.currentTarget.dataset.id;//文章id
    var app = getApp();
    var testUrl = app.testUrl;
    var that = this;
    var articleList;
    //获取所有文章
    wx.request({
      url: testUrl + '/article/subfavour.do',
      method: 'POST',
      data: {
        id: articleId,
        userid: app.globalData.openid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        if (res.data.success == true) {
          that.getdata()
        }
      }
    });
  },
 
  onShareAppMessage: function () {
    var that=this;
    return {
      title: that.data.article.title,
      desc: that.data.article.intro,
      path: '/pages/article/article?id='+that.article.id
    }
  }
})